var express = require('express');
var axios = require('axios');
var router = express.Router();

router.get('/', function (req, res, next) {
    // res.send("登陆页面");
    res.render('app/login');
})

/**
* @api {post} /admin/login/login 用户登录
* @apiName 用户登录
* @apiGroup User
*
* @apiParam {String} phone 手机号.
* @apiParam {String} password 密码.
*
* @apiSuccess {String} firstname Firstname of the User.
* @apiSuccess {String} lastname  Lastname of the User.
*/
router.post('/login', (req, res) => {
    let { phone, password } = req.body
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9022',
        url: '/tb-user/login',
        params: {
            phone: phone,
            password: password
        },
        headers: {//指定响应头
            "Content-Type": "application/json;charset=utf-8",
            "Accept": "application/json"
        }
    }
    axios(config)
        .then(function (response) {
            console.log(response);
            if (response.data.code == 'success') {
                console.log("成功")
                req.session.login = true
                req.session.user = response.data.data
                // req.session.phone = response.data.data.mobliePhone
                // req.session.userId = response.data.data.id
                // req.session.nickname = response.data.data.nickname
                // req.session.userId = response.data.data.userId
                // req.session.userId = response.data.data.userId
                res.send({ err: 0, msg: "登录成功" })
                res.redirect('app/index');
            } else {
                console.log(response);
                res.send({ err: -1, msg: "密码错误" })
                console.log("失败")
            }
        })
        .catch(function (error) {
            console.log(error);
            res.send({ err: -1, msg: "登录失败" })
            console.log("失败")
        });
})


module.exports = router;
